/*
 * https://github.com/morethanwords/tweb
 * Copyright (C) 2019-2021 Eduard Kuzmenko
 * https://github.com/morethanwords/tweb/blob/master/LICENSE
 */

@use "sass:math";

$bubble-margin: .125rem;
$bubble-margin-big: .5rem;
$bubble-overflow: math.div($bubble-margin-big, 8);
$bubble-overflow-big: math.div($bubble-margin-big, 2);
$bubble-beside-button-width: 38px;
$bubble-border-radius-medium: 6px;
$bubble-border-radius-big: 12px;

@keyframes bubbleSelected {
  0% {
    opacity: 0;
  }

  25% {
    opacity: 1;
  }

  to {
    opacity: 0;
  }
}

.bubbles-date-group {
  position: relative;

  /* .sticky_sentinel {
    visibility: visible;
    background: #000;
  } */

  .sticky_sentinel--top {
    /* Adjust the height and top values based on your on your sticky top position.
    e.g. make the height bigger and adjust the top so observeHeaders()'s
    IntersectionObserver fires as soon as the bottom of the sentinel crosses the
    top of the intersection container. */
    height: $bubble-overflow-big;
    top: 0;
  }

  // .sticky_sentinel--bottom {
  //   /* Height should match the top of the header when it's at the bottom of the
  //   intersection container. */
  //   height: 1px;
  //   bottom: 0;
  // }
}

.bubbles-group {
  position: relative;

  &-avatar {
    position: sticky !important;
    top: 0;
    bottom: $bubble-overflow-big;
    cursor: pointer;
    pointer-events: all;

    .bubbles.is-selecting & {
      transform: translate3d(0, 0, 0) scale3d(1, 1, 1);
      transform-origin: bottom;
    }

    &.is-gesturing-reply {
      transform: translateX(0);

      &.animating.backwards {
        transition: transform var(--transition-standard-out);
      }
    }

    &-container {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      display: flex;
      flex-direction: column-reverse;

      // make avatar clickable
      z-index: 2;
      pointer-events: none;
    }
  }
}

.bubble {
  --line-height: var(--messages-line-height);
  position: relative;
  z-index: 1;
  margin: 0 auto $bubble-margin;
  user-select: none;
  display: flex;
  flex-wrap: wrap;
  //flex-direction: column; // fix 'Unread messages', still need to refactor it

  &.is-gesturing-reply {
    transform: translateX(0);
    
    &.animating.backwards {
      transition: transform var(--transition-standard-out);
    }

    .bubble-gesture-reply-icon {
      width: var(--message-beside-button-size);
      height: var(--message-beside-button-size);
      position: absolute;
      top: 50%;
      right: var(--message-beside-button-margin);
      transform: translateY(-50%);
      color: #fff;
      border-radius: 50%;
      background-color: var(--message-highlightning-color);
      opacity: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      transform: translateY(-50%) scale(0.6);
      visibility: hidden;

      &:before {
        font-size: 1.125rem !important;
      }

      @include animation-level(2) {
        transition: transform .1s ease-in-out;
      }

      &.is-visible {
        visibility: visible;
        // opacity: 1;
        transform: translateY(-50%) scale(1);
      }
    }
  }

  &.is-highlighted, 
  &.is-selected, 
  /* .bubbles.is-selecting */ & {
    &:after {
      position: absolute;
      left: -50%;
      /* top: 0;
      bottom: 0; */
      top: -#{$bubble-overflow};
      bottom: -#{$bubble-overflow};
      content: " ";
      z-index: -1;
    }
  }

  /* &.is-highlighted, &.is-selected {
    &:not(.is-group-last):after {
      height: calc(100% + #{$bubble-overflow}) !important;
    }

    & + &:not(.is-group-last) {
      &:after {
        top: .125rem !important;
        height: calc(100% - #{$bubble-overflow}) !important;
      }
    }
  } */

  // ! if turn this on, there will be an empty space
  /* &.is-highlighted, &.is-selected {
    &.is-group-last:after {
      bottom: #{$bubble-overflow} !important;
    }
  } */

  &.is-highlighted:after {
    //background-color: rgba(0, 132, 255, .3);
    background-color: var(--message-highlightning-color);

    body:not(.animation-level-0) & {
      animation: bubbleSelected 2s linear;
    }
  }

  &.is-first-unread {
    @include respond-to(handhelds) {
      max-width: unset;
    }

    &:before {
      content: "Unread messages";
      height: 30px;
      margin-bottom: $bubble-overflow-big;
      margin-left: -50%;
      margin-right: -50%;
      text-align: center;
      color: var(--primary-color);
      line-height: 2.1;
      font-weight: var(--font-weight-bold);
      font-size: 15px;
      background-color: var(--surface-color);
      z-index: 2;
      position: relative;
    }

    &.is-highlighted,
    &.is-selected {
      &:after {
        top: calc(#{$bubble-overflow} + 30px);
      }
    }
  }

  &.is-selected {
    &:after {
      background-color: var(--message-highlightning-color);
    }

    body:not(.animation-level-0) & {
      &:after {
        animation: fade-in-opacity .2s linear forwards;
      }

      &.backwards:after {
        animation: fade-in-backwards-opacity .2s linear forwards;
      }
    }
  }

  //&.is-highlighted:after, &.is-first-unread:before, &.is-selected:after {
  &:after, 
  &:before {
    width: 200%;
    display: block;
  }

  &.is-multiple-documents {
    &:before, 
    &:after {
      display: none;
    }

    .user-avatar {
      z-index: 2;
    }
  }

  .replies-footer + .poll-message .poll-footer-button {
    border-radius: 0 !important;
  }

  /* &.channel-post {
    .message.poll-message {
      .time {
        position: unset;
      }
    }

		poll-element .poll-footer {
			height: auto;
		}
  } */

  &-select-checkbox {
    z-index: 3;
    position: absolute;
    left: 0;
    display: flex;
    margin: 0;
    
    .checkbox-box-border {
      border-color: var(--message-checkbox-border-color);
    }

    .checkbox-box-background {
      background-color: var(--message-checkbox-color);
    }

    .checkbox-box-check {
      use {
        stroke-width: 3.5;
      }
    }
  }

  & > &-select-checkbox {
    //bottom: .75rem; // * by avatar
    bottom: .3125rem; // * by middle of one-line message
    /* left: 0;
    top: 50%;
    transform: translateY(-50%); */
    .checkbox-box {
      box-shadow: 0px 0px 3px 0px rgba(0, 0, 0, .4);
    }
  }

  .bubbles.is-selecting &:not(.is-album) {
    .audio, 
    .document, 
    .attachment, 
    poll-element {
      pointer-events: none !important;
    }
  }

  // ! hide context menu for media on android
  .bubbles.is-selecting & {
    img, 
    video,
    a {
      pointer-events: none;
    }
  }
  
  &-content {
    //min-width: 60px;
    min-width: 56px;
    max-width: 100%;
    border-radius: $bubble-border-radius-big;
    box-shadow: 0 1px 2px 0 rgba(16, 35, 47, .15);
    position: relative;
    display: flex;
    flex-direction: column-reverse;
    // align-items: flex-start;
    z-index: 2;
    user-select: none;
    background-color: var(--message-background-color);

    body.animation-level-0 & {
      transition: none;
    }

    /* html.no-touch .bubbles:not(.is-selecting) &,
    html.is-touch .bubbles.is-selecting:not(.no-select) & {
      user-select: text;
    } */
    html.no-touch .chat:not(.no-forwards) .bubbles:not(.is-selecting) &,
    html.is-touch body:not(.no-select) .chat:not(.no-forwards) .bubbles.is-selecting & { // * need no-select on body because chat-input transforms in channels
      user-select: text;
    }

    &.hover-reaction-visible {
      .bubble-beside-button {
        transform: translateX(1.125rem);

        .bubble.is-out & {
          transform: translateX(-1.125rem);

          &.goto-original {
            transform: translateX(-1.125rem) rotate(180deg);
          }
        }
      }
    }
  }

  &-hover-reaction {
    --size: 1.875rem;
    --offset: calc(var(--size) * -.75);
    position: absolute;
    right: var(--offset);
    // bottom: -.0625rem;
    bottom: 0;
    width: var(--size);
    height: 1.625rem;
    border-radius: var(--size);
    z-index: 2;
    background-color: var(--surface-color);
    cursor: pointer;
    opacity: 0;
    transform: scale(.8);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--menu-box-shadow);

    @include animation-level(2) {
      transition: opacity var(--btn-corner-transition), transform var(--btn-corner-transition);
    }

    &.is-visible {
      &:not(.backwards) {
        transform: scale(1);
        opacity: 1;
      }
    }

    &-sticker {
      width: 1.125rem;
      height: 1.125rem;
      position: relative;
    }
  }

  /* &.with-beside-button &-content {
    @include respond-to(handhelds) {
      max-width: calc(100% - var(--message-handhelds-margin)) !important;
    }
  } */

  /* &.with-beside-button &-content {
    margin-right: calc(var(--message-beside-button-margin) * -1);
  } */
  
  &.service {
    //padding: 1rem 0;
    //padding: $bubble-margin 0;
    max-width: var(--messages-container-width);

    .bubble-content {
      /* justify-self: center; */
      margin: 0 auto;
      max-width: 100%;
      box-shadow: none;
    }
  }

  &.is-date {
    position: sticky;
    top: $bubble-overflow-big;
    padding-bottom: #{$bubble-overflow-big + $bubble-margin};
    //z-index: 3;
    z-index: 2;
    transition: opacity .3s ease;
    opacity: .99999; // for safari
    pointer-events: none;
    font-weight: var(--font-weight-bold);

    &:before, 
    &:after {
      display: none;
    }

    body.animation-level-0 & {
      transition: none;
    }

    &.is-sticky {
      opacity: .00001; // for safari
      // position: sticky;

      .bubbles-inner:not(.is-scrolling) & {
        //transition-delay: 1.35s;

        .bubble-content {
          cursor: default;
        }
      }

      /* .bubbles-inner.is-scrolling & {
        transition-delay: 0;
      } */
    }

    .chat[data-type="chat"] & .bubble-content,
    .chat[data-type="discussion"] & .bubble-content {
      cursor: pointer;
      pointer-events: all;
    }

    .bubbles:not(.has-sticky-dates) & {
      visibility: hidden;
    }

    .bubbles.has-sticky-dates &.is-fake {
      display: none;
    }

    &.is-fake {
      position: absolute;
      top: 0;
      left: 50%;
      transform: translateX(-50%);
      opacity: 1 !important;
      transition: none !important;
      visibility: visible !important;
    }
  }

  &-beside-button {
    //opacity: 0;
    //transition: .2s opacity;
    position: absolute;
    right: var(--message-beside-button-margin);
    bottom: 0;
    width: var(--message-beside-button-size);
    height: var(--message-beside-button-size);
    font-size: 1.5rem;
    align-items: center;
    display: flex;
    justify-content: center;
    color: var(--badge-text-color);
    border-radius: 50%;
    //background: rgba(0, 0, 0, .16);
    background: var(--message-highlightning-color);
    cursor: pointer;
    transform: translateX(0);

    html.no-touch & {
      opacity: 0;
    }

    html.no-touch body.animation-level-2 & {
      transition: opacity .2s ease-in-out, transform .2s ease-in-out;
    }
  }

  &-view-button {
    border-radius: 6px;
    border: 1px solid var(--message-primary-color);
    color: var(--message-primary-color);
    height: 2rem;
    margin: 0 0.5625rem .5rem;
    width: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none !important;

    @include hover() {
      background-color: var(--light-filled-message-primary-color);
    }

    .c-ripple__circle {
      background-color: var(--message-primary-color);
      opacity: $hover-alpha;
    }

    .message & {
      margin: .375rem 0 -.375rem;
      width: 100%;
      max-width: 100%;

      @include text-overflow(true);
    }

    & + .reactions-block {
      margin-top: .375rem;
    }
  }

  @include hover() {
    .bubble-beside-button {
      opacity: 1;
    }
  }

  .forward {
    font-size: 1.25rem;
  }

  /* &.is-group-first {
    padding-top: 10px;
  } */

  &.is-group-last {
    margin-bottom: #{$bubble-margin-big};

    &:after {
      bottom: -#{$bubble-overflow-big};
    }

    /* > .bubble-select-checkbox {
      bottom: .3125rem;
    } */

    .bubbles-inner.is-chat &.is-in {
      > .bubble-select-checkbox {
        bottom: .1875rem;
      }
    }
  }

  &.is-group-first {
    &:after {
      top: -#{$bubble-overflow-big};
    }
  }

  &:not(.forwarded):not(.must-have-name) {
    &:not(.is-group-first) {
      .name {
      // .bubble-content > .name, 
      // .document-wrapper > .name {
        display: none;
      }

      &:not(.is-message-empty):not(.is-reply) .message {
        padding-top: 6px;
      }
    }
  }

  &:not(.is-group-last) .bubble-content-wrapper > .user-avatar {
    display: none;
  }

  /* &:not(.hide-name) {
    .audio {
      margin: 4px 0;
    }
  } */

  /* .goto-original {
    opacity: 1;
  } */

  &.photo, 
  &.video {
    .bubble-content {
      width: min-content;
    }
    
    .web {
      /* width: max-content; */ // commented 10.02.2020
      /* width: min-content; */
      max-width: 100%;
    }

    .message {
      max-width: 420px;
    }
  }

  &.webpage {
    .preview-with-document {
      margin-bottom: 0 !important;
      min-width: 100%;
    }

    .document {
      height: 3.375rem;

      @include respond-to(handhelds) {
        padding-left: 44px;
      }
    }

    .has-document {
      display: flex;
      flex-direction: column;

      .preview-resizer {
        order: 1;
      }
    }
  }

  .preview-resizer {
    display: flex;
  }
  
  img.emoji {
    height: 18px;
    width: 18px;
    // margin: 0 .05rem;
    vertical-align: bottom;
  }

  .thumbnail {
    position: absolute;
  }

  .canvas-thumbnail {
    border-radius: inherit;
  }

  .canvas-dots {
    z-index: 1;
  }
  
  &.emoji-big {
    --emoji-size: 1rem;
    font-size: 0;

    .bubble-content {
      line-height: 1;
    }

    .attachment {
      --custom-emoji-size: var(--emoji-size);
      border-radius: 0 !important;

      img.emoji {
        width: var(--emoji-size);
        height: var(--emoji-size);
        max-width: 64px;
        max-height: 64px;
      }

      .custom-emoji,
      .emoji {
        display: inline-block;
      }
    }

    &:not(.sticker) {
      .attachment {
        padding-top: .25rem;
        padding-bottom: 1.25rem;
        //max-width: fit-content!important;
        max-height: fit-content!important;

        display: block;
        white-space: pre-wrap;
        word-break: break-word;
        font-size: var(--emoji-size);
        
        span.emoji {
          height: auto;
          width: auto;
        }
      }

      // .chat:not(.no-forwards) & {
      //   .attachment {
      //     cursor: text;
      //     user-select: text;
      //   }
      // }

      .message {
        margin-top: -1.125rem;
      }

      .bubble-content {
        max-width: unquote('min(420px, 100%)');
      }
    }
    
    /* &.sticker .bubble-content {
      max-width: 140px !important;
      max-height: 140px !important;
      user-select: none !important;
    } */
  }
  
  &.just-media {
    .bubble-content {
      // cursor: pointer;
      background: none!important;
      box-shadow: none;
      /* max-width: 300px;
      max-height: 300px; */
    }
    
    img {
      object-fit: contain;
    }

    &.is-message-empty .message {
      //background-color: rgba(0, 0, 0, .23);
      background: var(--message-highlightning-color);
    }
    
    /* &.is-message-empty .message {
      display: none;
    }
    
    &.is-message-empty:hover .message {
      display: block;
    } */
  }

  &.sticker,
  &.emoji-big:not(.sticker) {
    .bubble-content {
      align-self: flex-start;
    }

    .message {
      position: relative !important;
      // align-self: flex-start;
      margin-left: auto;
      right: 0 !important;
    }
  }

  &.sticker {
    .attachment {
      position: absolute;
      border-radius: 0;
      z-index: 1;
    }

    /* .bubble-content {
      max-width: 200px !important;
      max-height: 200px !important;
    } */
  }

  &.round {
    .attachment {
      max-width: var(--round-video-size) !important;
      max-height: var(--round-video-size) !important;
      width: var(--round-video-size) !important;
      height: var(--round-video-size) !important;
    }

    .media-photo, 
    .media-video {
      border-radius: 50%;
      pointer-events: none;
    }
  }

  &:not(.is-message-empty) .attachment/* ,
  &:not(.is-message-empty).is-reply .attachment */ {
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
  }
  
  &:not(.emoji-big) .attachment {
    font-size: 0;
  }
  
  .attachment {
    max-width: 100%;
    border-radius: inherit;
    overflow: hidden;
    user-select: none;

    //-webkit-mask-image: -webkit-radial-gradient(circle, white 100%, black 100%); // fix safari overflow
    
    display: flex; // lol
    justify-content: center;
    position: relative;
    cursor: pointer;
    
    img, 
    video {
      max-width: 100%;
    }

    .download {
      position: absolute;
      left: 0;
      top: 0;
      height: 100%;
      width: 100%;
      cursor: pointer;
      display: flex;
      justify-content: center;
      align-items: center;

      span {
        background-color: var(--message-time-background);
        font-size: 23px;
        color: #fff;
        text-align: center;
      }
    }

    .download, 
    .preloader-container {
      & ~ .video-play {
        display: none;
      }
    }
  }

  .media-container {
    &-aspecter {
      position: relative;
      margin: 0 auto;
      z-index: 1;
    }

    &-fitted {
      background-color: transparent !important;

      > .thumbnail {
        opacity: .8;

        &.fade-in {
          animation: thumbnail-fade-in-opacity .2s ease-in-out forwards;
        }
      }
    }
  }

  .preloader-container {
    z-index: 2;
  }
  
  &:not(.sticker) {
    .attachment {
      max-width: unquote('min(420px, 100%)');
      max-height: unquote('min(340px, 100%)');

      width: max-content;
    }
  }

  img:not(.emoji), 
  video {
    /* object-fit: contain; */
    object-fit: cover;
    width: 100%;
    height: 100%;
  }

  html.is-safari &:not(.round) {
    img:not(.emoji), 
    video {
      border-radius: inherit;
    }
  }

  &.is-album {
    .attachment {
      max-width: unquote('min(451px, 100%)');
      max-height: none;
      //overflow: hidden;
    }
  }

  .album-item {
    background-color: var(--message-highlightning-color);
    max-width: 100%;
    cursor: pointer;
    position: absolute;
    overflow: hidden;

    @include animation-level(0) {
      transition: none !important;
    }

    /* .bubbles.is-selecting & {
      -webkit-mask-image: -webkit-radial-gradient(circle, white 100%, black 100%); // fix safari overflow
    } */

    img, video {
      border-radius: inherit;
    }

    &-media { // * fix overflow for handhelds
      border-radius: inherit;

      @include animation-level(0) {
        transition: none !important;
      }
    }

    .bubble-select-checkbox {
      bottom: auto !important;
      left: auto;
      right: .5rem;
      top: .5rem;
    }

    &.is-selected {
      border-radius: 0;
      
      .album-item-media {
        transform: scale(1);
      }

      &.animating {
        transition: border-radius var(--transition-standard-out);

        .album-item-media {
          transition: transform var(--transition-standard-out), border-radius var(--transition-standard-out);
        }
      }

      &:not(.backwards) {
        transition: border-radius var(--transition-standard-in);
        
        .album-item-media {
          transition: transform var(--transition-standard-in), border-radius var(--transition-standard-in);
          transform: scale(.883333);
        }

        &, .album-item-media {
          //border-radius: .5rem;
          border-radius: 0;
        }
      }
    }
  }

  a {
    @include hover() {
      text-decoration: underline;
    }
  }

  &-first {
    order: -1;
    margin-top: .5rem;
    justify-content: center;
    align-items: center;
    max-width: 100%;
    
    @include animation-level(0) {
      flex: 1 1 auto;
    }

    .attachment {
      pointer-events: none;
    }

    .bubble-content-wrapper {
      max-width: 100%;
    }

    &.empty-bubble-placeholder {
      position: absolute;
      top: 50%;
      // transform: translateY(-50%);
      left: 50%;
      transform: translate(-50%, -50%);

      @include animation-level(2) {
        .bubble-content-wrapper {
          transition: var(--bubble-transition-in);
        }
      }

      &.has-description {
        .service-msg {
          flex-direction: column;
          align-items: flex-start !important;
          padding: .75rem 1rem .875rem !important;
        }
  
        .center {
          align-self: center;
        }
  
        .empty-bubble-placeholder-title {
          font-weight: var(--font-weight-bold);
          font-size: 1rem !important;
        }

        .bubble-content {
          border-radius: 1.5rem !important;
        }
      }

      .empty-bubble-placeholder-line {
        color: #fff;
      }

      .empty-bubble-placeholder-line + .empty-bubble-placeholder-line {
        margin-top: .5rem;
      }

      .tgico-check {
        margin-right: .25rem;
        font-size: 1.25rem;
        vertical-align: bottom;
        margin-left: -.1875rem;
      }

      .empty-bubble-placeholder-list-bullet {
        margin-right: .3125rem;
      }

      .bubbles.has-groups & {
        pointer-events: none;
        
        .bubble-content-wrapper {
          transform: scale3d(.8, .8, 1) translateX(0);
          //transform: scale(.8) translateX(0);
          opacity: 0;
        }
      }

      &.empty-bubble-placeholder-group {
        .empty-bubble-placeholder-list-item {
          margin-top: .4375rem !important;
        }
      }

      &.empty-bubble-placeholder-greeting {
        .service-msg {
          max-width: 232px;
        }

        .empty-bubble-placeholder-subtitle {
          margin-top: .25rem !important;
        }
      }

      .empty-bubble-placeholder-sticker {
        margin-top: .75rem !important;
        position: relative;
        width: 200px;
        height: 200px;
        cursor: pointer;
      }
    }

    .time {
      display: none !important;
    }

    &:before, &:after {
      display: none;
    }
  }

  .web {
    // margin: .125rem 0;
    margin: .125rem 0 0;
    max-width: 100%;
    overflow: hidden;
    line-height: var(--line-height);

    & + .time {
      display: block;
    }
    
    .preview {
      max-width: unquote('min(420px, 100%)');
      max-height: unquote('min(340px, 100%)');
      border-radius: 4px;
      overflow: hidden;
      user-select: none;
      cursor: pointer;
      position: relative;
      width: max-content;
      
      img, video {
        max-width: 100%;
      }

      &-resizer {
        &:first-child {
          margin: 3px 0;

          &:last-child {
            margin-bottom: 2px;
          }
        }
      }

      &.is-round {
        .media-photo,
        .media-video {
          border-radius: 50%;
        }
      }
    }

    &.no-text {
      margin-bottom: .75rem;
    }
    
    .title {
      &:not(:first-child) {
        margin-top: 1px;
      }
    }

    .webpage-name,
    .text,
    .title {
      font-size: var(--messages-secondary-text-size);
      line-height: var(--messages-secondary-line-height);
    }
    
    .webpage-name {
      text-decoration: none;
      
      @include hover() {
        text-decoration: underline;
      }
    }
    
    .text {
      word-break: break-word;
      margin-top: 1px;
    }

    .quote {
      max-width: 100%;
      overflow: hidden;
      width: 100%;
      display: flex;

      &-text {
        width: 100%;
        // max-width: calc(100% - .625rem); // left border
        max-width: 100%;
        padding-left: .625rem;
        margin-left: -.625rem;
      }

      &:before {
        flex: 0 0 auto;
        width: .125rem;
        border-radius: .125rem;
        background-color: var(--primary-color);
        margin: .1875rem .5rem .125rem 0;
        content: " ";
      }
    }
  }
  
  .reply {
    --font-size: var(--messages-secondary-text-size);
  }

  .document {
    &-name {
      font-size: var(--messages-text-size);
    }

    &-size {
      font-size: var(--messages-secondary-text-size);
    }
  }

  &.is-square-photo {
    .bubble-content {
      width: fit-content;
    }

    .web {
      .webpage-name {
        margin-right: 1rem;
      }

      .preview-resizer {
        margin: 0;
        width: 3rem;
        height: 3rem;
        float: right;
        margin-left: .625rem;
        margin-top: .1875rem;
      }

      .preview {
        max-width: unquote('min(3rem, 100%)');
        max-height: unquote('min(3rem, 100%)');
        margin: 0;
      }
    }
  }

  /* &.is-vertical-photo {
    .bubble-content {
      width: fit-content;
    }
  } */

  .reply {
    padding: .25rem;
    margin: .375rem;
    margin-top: 0;
    cursor: pointer;
    border-radius: .25rem;
    min-width: 10rem;
    width: auto;

    

    &-media {
      top: .125rem;
    }

    &-content {
      //max-width: 300px;
      position: absolute;
      max-width: calc(100% - 1.25rem);
      height: auto;
      min-height: 32px;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
  }

  // &:not(.just-media):not(.is-message-empty) .reply {
  .message + .reply {
    // margin-bottom: .125rem; // ! JS2 mockup
    margin-bottom: 0; // ! same margin as between name and message
  }

  &:not(.just-media) {
    .reply {
      border-radius: $bubble-border-radius-medium;
      // border-top-left-radius: $bubble-border-radius-medium;
      // border-top-right-radius: $bubble-border-radius-medium;

      @include hover() {
        background-color: var(--light-filled-message-primary-color);
      }
    }
  }

  &.just-media {
    .floating-part {
      padding: .375rem .625rem;
      border-radius: 12px;
      position: absolute;
      top: 0;
      // margin-bottom: 0;
      margin: 0;
      background-color: var(--message-highlightning-color);
      white-space: nowrap;
      max-width: 15rem;

      @include respond-to(handhelds) {
        max-width: calc(100vw - #{$chat-padding-handhelds * 2} - 10px - 100%);
      }
    }

    .name {
      color: #fff !important;
      padding: .375rem .625rem;
      line-height: var(--line-height);

      .peer-title {
        font-weight: 400 !important;
        // pointer-events: none;
      }
    }

    .reply {
      height: 54px;
      max-height: 54px;

      @include respond-to(handhelds) {
        padding: 8px 6px 8px 8px;
      }

      &-content {
        margin-top: 0;
        position: relative;
        max-width: none !important;
      }

      &-title,
      &-subtitle, 
      i {
        color: #fff !important;
      }

      &-border {
        background: #fff !important;
      }
    }
  }

  /* &.photo, &.video {
    &:not(.hide-name) {
      .attachment {
        border-top-left-radius: 0;
        border-top-right-radius: 0;
      }
    }
  } */

  &.forwarded .attachment,
  &.is-reply .attachment,
  &:not(.hide-name)/* .is-message-empty */ .attachment/* ,
  &:not(.hide-name):not(.sticker) .attachment */ {
    border-top-left-radius: 0;
    border-top-right-radius: 0;
  }

  .message,
  .service-msg {
    --custom-emoji-size: var(--messages-custom-emoji-size);
  }

  .message {
    font-size: var(--messages-text-size);
    padding: 0 .5rem .375rem .625rem;
    max-width: 100%;
    color: var(--primary-text-color);
    line-height: var(--line-height); // 21 / 16
    word-break: break-word;
    white-space: pre-wrap; // * fix spaces on line begin
    position: relative;

    .custom-emoji {
      display: inline-flex; // fix RTL thumb absolute position
    }

    &:last-child {
      padding-top: 6px;
    }
  }

  @include respond-to(handhelds) {
    .document, 
    .audio {
      --icon-size: 2.25rem;
    }
  }

  .audio {
    $parent: ".audio";
    #{$parent} {
      &-title {
        font-weight: var(--font-weight-bold);
      }

      &-ico {
        font-size: 0;
      }
    }

    @include respond-to(handhelds) {
      height: 2.375rem;
      --icon-margin: .6875rem;

      .audio-details {
        margin-top: 2px;
        margin-bottom: 0;
      }

      .audio-to-text-button {
        margin-top: -.875rem;
      }

      .audio-subtitle {
        margin-top: -1px;
      }

      /* &.corner-download .audio-download {
        margin: 1.375rem 1.375rem 0;
      } */
    }
  }

  .message.audio-message {
    width: 335px;

    @include respond-to(handhelds) {
      width: 280px;
    }
  }

  .message.audio-message,
  .message.voice-message {
    // width: 335px;
    max-width: unquote("min(100%, 364px)") !important;

    @include respond-to(handhelds) {
      // width: 280px;
      max-width: unquote("min(100%, 300px)") !important;
    }
  }

  &.min-content {
    .bubble-content {
      width: min-content;
    }
  }

  .message.voice-message {
    .document-message {
      max-width: fit-content;
    }
  }

  .message.audio-message, 
  .message.voice-message {
    padding: 8px !important;
    //padding: 4px 6px 4px 8px !important;
  }

  .message.contact-message,
  .message.call-message {
    min-width: 200px;
    padding-left: 8px;
  }

  .message.contact-message {
    .contact {
      display: flex;
      padding: 2px 0;
      cursor: pointer;
      user-select: none;

      &-avatar {
        color: #fff;
      }

      &-details {
        padding-left: 12px;
        margin-top: 8px;
        display: flex;
        flex-direction: column;
      }

      &-name {
        font-size: 1rem;
        font-weight: var(--font-weight-bold);
      }

      &-number {
        font-size: 14px;
        line-height: 1.4;
      }
    }
  }

  .message.poll-message {
    max-width: 400px;
  }

  .message.document-message {
    max-width: 325px !important;

    .document {
      --icon-margin: .75rem;
      height: 58px;

      @include respond-to(handhelds) {
        --icon-margin: .5rem;
        height: 44px;

        .document-size {
          font-size: .75rem;
        }

        &:not(.document-with-thumb) .document-ico {
          padding: 1.125rem 0 0 0;
        }
      }

      &-name {
        line-height: 1.4;
        margin-top: 1px;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }

  &-call {
    cursor: pointer;
    padding: .125rem 0;
    padding-left: 2.5625rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    position: relative;
    user-select: none;

    &:before {
      position: absolute;
      left: .3125rem;
      font-size: 1.5rem;
    }

    &-title {
      font-weight: var(--font-weight-bold);
    }

    &-subtitle {
      font-size: var(--messages-secondary-text-size);
      color: var(--secondary-text-color);
      display: flex;
      align-items: center;
      margin-top: .0625rem;
      margin-left: -.1875rem;
      line-height: var(--messages-secondary-line-height);

      &.is-reason:before {
        margin-right: .0625rem;
      }

      &:before {
        content: $tgico-arrow-next;
        display: inline-block;
        transform: rotate(135deg);
        font-size: 1rem;
      }

      &.arrow-red:before {
        color: var(--danger-color);
      }

      &.arrow-green:before {
        color: var(--message-out-primary-color);
      }
    }
  }

  .document-wrapper {
    display: flex;
    flex-direction: column-reverse;

    .document-message {
      margin-top: .25rem;
    }
  }

  .document {
    .time {
      height: 0;
      align-self: flex-start;
    }
  }

  /* .document,
  .audio {
    .time.tgico {
      position: relative !important;
      height: 0px !important;
      visibility: hidden !important;
      float: none;
  
      .inner {
        visibility: hidden !important;
      }
    }
  } */

  .document-message {
    & + .document,
    & + .audio {
      .time {
        display: none !important;
      }
    }
  }

  &.is-multiple-documents {
    /* .bubble-content {
      position: unset;
    } */

    .message {
      padding: 0 !important;
      border-radius: inherit;
    }

    .document-container {
      position: relative;
      border-radius: inherit;

      .document-selection {
        position: absolute;
        top: 0;
        bottom: 0;
        z-index: -2;
        width: 200vw;
        left: -75vw;
      }

      &.is-highlighted, 
      &.is-selected {
        .document-wrapper {
          &:before {
            content: " ";
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            background-color: var(--message-background-color);
            border-radius: inherit;
            z-index: -1;
          }
        }
      }

      &.is-highlighted {
        .document-selection {
          background-color: var(--message-highlightning-color);
        }

        body:not(.animation-level-0) & {
          .document-selection/* , 
          .document-wrapper:before */ {
            animation: bubbleSelected 2s linear;
          }
        }
      }

      &.is-selected {
        .document-selection {
          background-color: var(--message-highlightning-color);
        }

        body:not(.animation-level-0) & {
          .document-selection/* , 
          .document-wrapper:before */ {
            animation: fade-in-opacity .2s linear forwards;
          }
  
          &.backwards {
            .document-selection/* , 
            .document-wrapper:before */ {
              animation: fade-in-backwards-opacity .2s linear forwards;
            }
          }
        }
      }

      .bubble-select-checkbox {
        --margin-top: .25rem;
        --margin-left: .125rem;
        left: auto;
        top: auto;
        background: #fff;
        border-radius: 50%;
        margin-left: calc(var(--padding-left) * -1 + var(--icon-size) - var(--size) + var(--margin-left));
        margin-top: calc(var(--icon-size) - var(--size) + var(--margin-top));

        @include respond-to(handhelds) {
          --size: 1.125rem;
          // left: 20px;
          // top: 25px;
        }

        &:before {
          --inner-size: .125rem;
          content: " ";
          position: absolute;
          width: calc(var(--size) - (var(--inner-size) * 2));
          height: calc(var(--size) - (var(--inner-size) * 2));
          left: var(--inner-size);
          top: var(--inner-size);
          border: 2px solid var(--secondary-color);
          border-radius: inherit;
        }
      }

      &:first-of-type {
        .document-selection {
          top: -#{$bubble-overflow}; // * padding inner + half padding outer
        }

        .document-wrapper {
          padding-top: .5rem;
          border-top-left-radius: inherit;
          border-top-right-radius: inherit;
        }
      }

      &:last-of-type {
        .document-selection {
          bottom: -#{$bubble-overflow};
        }

        .document-wrapper {
          padding-bottom: .5rem;
          border-bottom-left-radius: inherit;
          border-bottom-right-radius: inherit;
        }
      }
    }

    &.is-group-first .document-container {
      &:first-of-type {
        .document-selection {
          top: -$bubble-overflow-big;
        }
      }
    }

    &.is-group-last .document-container {
      &:last-of-type {
        .document-selection {
          bottom: -$bubble-overflow-big;
        }
      }
    }

    .document {
      height: 54px !important;
      
      @include respond-to(handhelds) {
        height: 38px !important;
      }
    }

    .document-wrapper {
      padding: .25rem .5rem;

      // &:before {
      //   position: absolute;
      //   top: 0;
      //   right: 0;
      //   bottom: 0;
      //   left: 0;
      //   border-radius: inherit;
      //   background-color: var(--message-background-color);
      //   content: " ";
      // }

      > .name {
        // padding: .0625rem 0 .375rem;
        padding: 0 0 .25rem 0;
        margin-top: -.1875rem;
      }
    }

    // * if have name
    /* .bubbles-inner.is-chat &.is-in.is-group-first {
      .document-container:first-of-type {
        .document-selection {
          top: -30px;
        }

        .document-wrapper {
          border-top-left-radius: 0;
          border-top-right-radius: 0;

          &:before {
            top: -26px;
            border-top-left-radius: $border-radius-big;
            border-top-right-radius: $border-radius-big;
          }
        }
      }
    } */
  }

  .message {
    // &.document-message, 
    // &.audio-message, 
    // &.voice-message, 
    .audio, 
    &.poll-message, 
    &.contact-message {
      .time {
        position: absolute;
        right: 0;
        bottom: 0;
      }
    }
  }

  // ! SAFARI FIX BLINK ON SELECTION TRANSFORM !
  html:not(.is-safari) & {
    .message.voice-message {
      overflow: hidden;
    }
  }

  &.is-message-empty {
    .bubble-content-wrapper {
      width: min-content;
    }

    .message {
      position: absolute;
      /* position: relative;
      width: max-content; */
      bottom: .1875rem;
      right: .1875rem;
      border-radius: .75rem;
      background-color: var(--message-time-background);
      padding: 0 .3125rem;
      z-index: 2;

      .time {
        margin-left: 0;
        display: flex;
        align-items: center;
        padding: 0;
        margin: 0;
        white-space: nowrap;
        height: calc(var(--messages-time-text-size) + .375rem);

        .inner {
          right: unset;
          bottom: unset;
          color: #fff;
          margin: inherit;

          &:after {
            color: #fff !important;
          }
        }
      }
    }

    &.with-replies:not(.sticker):not(.with-beside-replies) .message {
      bottom: 55px;
    }

    &.sticker, 
    &.with-replies.round, 
    &.emoji-big {
      .message {
        bottom: 0;
      }
    }

    &:not(.emoji-big) {
      .reactions-block {
        max-width: fit-content;
      }
    }

    .reaction-block {
      --chosen-background-color: var(--primary-color);

      &:not(.is-chosen) {
        @include hover() {
          &:after {
            background-color: #fff;
          }
        }
      }
    }
  }

  &.with-reply-markup {
    .bubble-content {
      min-width: 100%;
    }
  }

  &.with-replies .attachment {
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
  }

  &.with-beside-replies .bubble-content {
    min-height: 5.5rem;
  }

  .colored-name .premium-icon {
    opacity: .6;
    color: inherit;
  }
  
  .time {
    visibility: hidden; // * can't use color transparent here, because in name can be emoji
    font-size: var(--messages-time-text-size);
    height: var(--messages-time-text-size);
    user-select: none;
    line-height: 1;
    vertical-align: middle;
    pointer-events: none; // do not show title
    display: inline-flex;
    z-index: 1;
    /* display: inline-flex;
    align-items: center; */
    direction: ltr;
    float: right; // * rtl fix
    cursor: pointer;

    i {
      font-size: 1.125rem;
      margin-right: .375rem;
    }

    &-icon {
      pointer-events: none;
      
      &:not(:first-child) {
        margin-left: 2px;
      }
    }

    i.edited {
      overflow: visible;
      font-size: inherit;
    }
    
    .inner {
      pointer-events: all;
      position: absolute;
      bottom: 0;
      right: 0;
      display: flex;
      align-items: center;
      line-height: 1;
      padding: inherit;
      white-space: nowrap;
      height: var(--messages-time-text-size); // * as font-size
      visibility: visible;
      color: var(--message-time-color);

      &:after {
        color: var(--message-status-color);
      }
    }

    .tgico-pinnedchat {
      margin-right: .125rem;
    }

    &.is-block {
      float: none;
      display: block;
    }
  }

  &.webpage .time {
    float: none;
  }

  .video-time, 
  .video-play {
    pointer-events: none;
  }

  .video-time {
    --height: calc(var(--messages-time-text-size) + .375rem);
    position: absolute;
    top: 3px;
    left: 3px;
    border-radius: var(--height);
    background-color: var(--message-time-background);
    padding: 0px 6px;
    z-index: 2;
    font-size: var(--messages-time-text-size);
    color: white;
    display: flex;
    align-items: center;
    user-select: none;
    height: var(--height);
    line-height: 1;

    &.can-autoplay:after {
      content: $tgico-nosound;

      // * same as .iconVolume
      padding: 0 1px 0 3px;
      font-size: calc(var(--messages-time-text-size) + 8px);
      color: #fff;
    }

    &.is-error:after {
      content: $tgico-sendingerror;
      font-size: calc(var(--messages-time-text-size) + 4px);
      padding: 0;
      margin-inline-start: 1px;
    }
  }

  .extended-media-buy {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    height: 2.5rem;
    padding: 0 1rem;
    background-color: rgba(0, 0, 0, .3);
    backdrop-filter: var(--menu-backdrop-filter);
    font-weight: var(--font-weight-bold);
    color: #fff;
    z-index: 2;
    font-size: var(--font-size-14);
    border-radius: 2rem;
    white-space: nowrap;
    display: flex;
    align-items: center;

    &:before {
      margin-right: .25rem;
      font-size: 1.125rem;
    }
  }

  pre {
    display: inline;
    margin: 0;
  }

  .anchor-url {
    text-decoration: underline;
  }

  .video-play {
    background-color: var(--message-time-background);
    color: #fff;
    text-align: center;
    font-size: 2.125rem;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2;
  }

  .ckin__player.circle {
    z-index: 1;
  }

  &:not(.forwarded).hide-name {
    .name {
      display: none;
    }
  }
  
  .name {
    user-select: none;
  }
  
  .peer-title {
    cursor: pointer;
  }
  
  .name {
  // &-content > .name, 
  // .document-wrapper > .name {
    padding: .375rem .625rem 0 .625rem;
    font-weight: var(--font-weight-bold) !important;
    /* padding-bottom: 4px; */
    color: var(--primary-color);
    font-size: var(--messages-secondary-text-size);
    line-height: calc(var(--messages-secondary-text-size) + .25rem);
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    // order: 1;
    //width: max-content;
    //white-space: nowrap;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .badge-fake {
      display: inline-block;
      vertical-align: middle;
      margin-top: -2px;
    }
  }

  &-name-rank {
    color: var(--message-time-color);
    font-weight: var(--font-weight-normal);
    font-size: var(--messages-time-text-size);
    margin-inline-start: .125rem;
    @include text-overflow();
  }

  /* &:not(.is-group-first) .bubble-content > .name .name {
    display: none;
  } */
  
  &:not(.webpage) {
    &.photo, 
    &.video {
      &:not(.is-reply) .bubble-content > .name {
        //padding-bottom: .2675rem;
        padding-bottom: 6px;
      }
      
      &:not(.is-message-empty) .message {
        //padding-top: .2675rem;
        padding-top: 6px;
      }

      .topic-name-button-container {
        margin-bottom: 0;
      }
    }
  }

  &:not(.webpage):not(.is-album):not(.sticker):not(.round):not(.emoji-big) .attachment {
    background-color: #000;
  }
  
  &.hide-name:not(.is-reply):not(.is-message-empty) .message {
    //padding-top: .2675rem;
    padding-top: 6px;
  }

  &:not(.sticker):not(.emoji-big):not(.forwarded) {
    &.hide-name, 
    &:not(.is-group-first)/* , &.is-out */ {
      .reply {
        margin-top: 6px;
      }
    }
  }

  .bubble-tail {
    display: none;
  }
  
  //&.can-have-tail.is-group-last .bubble-content:after {
  &.can-have-tail.is-group-last .bubble-tail {
    position: absolute;
    //bottom: 0;
    width: 11px;
    height: 20px;
    //background-repeat: no-repeat no-repeat;
    //content: '';
    //background-size: 11px 20px;
    //background-position-y: 1px;
    z-index: -2;
    display: block;
    fill: var(--message-background-color);
    transform: translateY(1px);
  }

  &.photo, 
  &.video {
    &.is-message-empty.is-group-last:not(.with-replies) {
      //.bubble-content:after {
      .bubble-tail {
        display: none;
      }
    }
  }

  &.is-message-empty.is-group-last.with-media-tail {
    //.bubble-content:after {
    .bubble-tail {
      display: none;
    }

    .attachment {
      overflow: visible;
    }
  }

  &__media-container {
    cursor: pointer;
    border-radius: inherit;
  }

  audio-element, 
  poll-element {
    white-space: normal; // * fix due to .message white-space prewrap
  }

  .replies {
    user-select: none;

    .c-ripple__circle {
      background-color: var(--light-primary-color);
    }

    .rp {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      overflow: hidden;
      border-radius: inherit;
      cursor: pointer;
    }

    &-beside {
      flex-direction: column;
      width: 36px;
      min-height: 36px;
      height: auto;
      max-height: 52px;
      bottom: 47px; // * forward button + 9px margin
      border-radius: 2rem;
      right: -44px; // * because not 38px, as forward button
      padding: .375rem 0 .5rem;

      &-text {
        font-size: .75rem;
        margin-top: .125rem;
      }
    }

    &-footer {
      height: 3.0625rem;
      border-top: .0625rem solid var(--border-color);
      position: relative;
      display: flex;
      align-items: center;
      padding: 0 .5rem;
      border-bottom-left-radius: inherit;
      border-bottom-right-radius: inherit;
      color: var(--primary-color);
      min-width: 15rem;

      @include hover() {
        background-color: var(--light-primary-color);

        & ~ .bubble-tail {
          fill: var(--light-filled-message-primary-color);
        }

        // .tgico-next {
        //   animation: arrow-shake .3s ease-out forwards;
        // }
      }
  
      .tgico-comments, 
      .tgico-next {
        font-size: 1.5rem;
      }

      .tgico-comments {
        margin-left: .125rem;
        margin-right: .375rem;
      }
  
      &-text {
        font-weight: var(--font-weight-bold);
        font-size: .9375rem;
        margin-left: .5rem;
        display: flex;
        align-items: center;
        color: var(--primary-color);
        line-height: 1.125rem;
      }
  
      &-avatars {
        --margin-right: -.875rem;
        --border-size: 2px;
      }
  
      .tgico-next {
        position: absolute;
        right: .1875rem;
      }
  
      &.is-unread {
        .replies-footer-text {
          &:after {
            content: " ";
            background-color: var(--primary-color);
            width: .5rem;
            height: .5rem;
            margin-left: .75rem;
            border-radius: 50%;
          }
        }
      }
    }
  }

  &.is-thread-starter {
    .user-avatar {
      display: none;
    }

    &.is-in .bubble-content-wrapper {
      margin-left: 0;
    }
  }

  .message {
    .reaction {
      --background-color: var(--light-filled-message-primary-color);

      &:not(.is-chosen), 
      &.is-chosen.backwards {
        --counter-color: var(--message-primary-color);

        .stacked-avatars {
          --border-color: var(--background-color);
        }
      }
    }

    .reactions-block {
      .time {
        position: unset !important;
        right: auto !important;
        bottom: auto !important;
        order: 100;
      }
    }
  }

  code {
    cursor: pointer;
  }

  &.is-invoice {
    .attachment {
      background-color: inherit !important;
    }
  }

  &.with-reply-markup {
    .bubble-content {
      border-bottom-left-radius: $bubble-border-radius-medium !important;
      border-bottom-right-radius: $bubble-border-radius-medium !important;
    }
  }

  .name-with-reply {
    .name {
      padding: 0 !important;

      &:not(:last-child) {
        .hide-ol {
          display: none;
        }

        .peer-title {
          font-weight: var(--font-weight-bold) !important;
        }
      }
    }

    .reply {
      margin: .25rem 0 0;
      padding: 0;
      height: auto;
      max-height: none;
    }
  }

  .topic-name-button {
    --color: var(--message-primary-color);
  }

  // .name .peer-title + .topic-name-button-container {
  //   margin-top: .25rem;
  // }

  .name .topic-name-button-container {
    margin: .25rem -.125rem;

    &:first-child {
      margin-top: 0;
    }
  }

  &.is-message-empty:not(.is-reply) .name .topic-name-button-container {
    margin-bottom: 0;
  }

  &:not(.is-group-first) .topic-name-button-container {
    display: none !important;
  }

  .topic-name-button-container.floating-part {
    padding-top: 0;
    padding-bottom: 0;
    font-size: var(--messages-secondary-text-size);

    .topic-name-button {
      --color: #fff;
      padding: 0;

      &:before {
        content: none;
      }
    }

    & + .floating-part {
      margin-top: 1.75rem;
    }
  }

  .web .audio-transcribed-text {
    margin-bottom: 0;
    order: 4;
  }

  .audio-transcribed-text {
    margin-bottom: .75rem;
    margin-top: .25rem;

    &.is-error {
      color: var(--message-primary-color);
    }

    .audio-transcribing-dots {
      position: relative;

      &:before,
      &:after {
        content: "...";
      }

      &:before {
        opacity: 0;
      }

      &:after {
        position: absolute;
        right: 0;
        left: 0;
        animation: 1.5s linear audio-dots infinite;
      }
    }
  }

  &-premium-gift {
    &-container {
      margin-top: .5rem !important;
    }

    &-wrapper {
      flex-direction: column;
      width: 12.5rem;
      height: 12.875rem;
    }

    &-sticker {
      margin-bottom: 10px;
      margin-top: -20px;
    }
  }

  // .document-message + .audio-transcribed-text {
  //   margin-bottom: .75rem;
  // }
}

.timestamp {
  text-decoration: underline;

  &.is-disabled {
    color: inherit;
    text-decoration: none !important;
    cursor: inherit;
  }
}

@keyframes audio-dots {
  0% {
    content: "";
  }

  33% {
    content: ".";
  }

  66% {
    content: "..";
  }

  100% {
    content: "...";
  }
}

// @keyframes arrow-shake {
//   0% {
//     transform: translateX(0);
//   }

//   25% {
//     transform: translateX(-.25rem);
//   }

//   75% {
//     transform: translateX(.125rem);
//   }

//   100% {
//     transform: translateX(0);
//   }
// }

// * fix scroll with only 1 bubble
.bubbles-date-group:last-of-type {
  .bubbles-group:last-of-type {
    .bubble:last-of-type {
      margin-bottom: $bubble-overflow;
      /* &:after, .document-container:last-of-type .document-selection {
        bottom: 0 !important;
      } */

      &:after,
      .document-container:last-of-type .document-selection {
        bottom: -#{$bubble-overflow};
      }
    }
  }
}

// .bubble-content-wrapper,
// .bubbles-group-avatar,
.can-zoom-fade {
  transform: scale(1) translateX(0);
  transform-origin: center;
  opacity: 1;

  @include animation-level(2) {
    transition: var(--bubble-transition-out);
  }

  .bubbles-inner.zoom-fading & {
    transition: var(--bubble-transition-in);
    //transition: transform 10s ease-in-out, opacity 10s ease-in-out;
  }

  &.zoom-fade /* .bubble-content */ {
    transform: scale3d(.8, .8, 1) translateX(0);
    //transform: scale(.8) translateX(0);
    opacity: 0;
  }
}

.bubble-content-wrapper {
  display: flex;
  flex-direction: column;

  .bubble:not(.service) & {
    @include respond-to(not-handhelds) {
      max-width: 85%;
    }
  }

  @include respond-to(handhelds) {
    max-width: calc(100% - var(--message-handhelds-margin));
  }
}

.bubble.service {
  align-self: center;
  justify-content: center;

  b {
    color: inherit;
  }

  .bubble-content {
    background-color: transparent;
    border-radius: .875rem;

    &-wrapper {
      max-width: 100%;
      padding: 0 2rem;
    }
  }
  
  .service-msg {
    color: #fff;
    //background-color: rgba(0, 0, 0, .24);
    background-color: var(--message-highlightning-color);
    font-size: var(--messages-service-text-size);
    padding: .28125rem .625rem;
    // line-height: var(--line-height);
    line-height: calc(var(--messages-service-text-size) + 5px);
    border-radius: inherit;
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    word-break: break-word;

    i {
      font-style: normal;
    }

    a {
      color: #fff;
    }

    a, 
    .peer-title,
    [data-saved-from] {
      cursor: pointer;
      font-weight: var(--font-weight-bold);

      &:hover {
        text-decoration: underline;
      }
    }

    img.emoji {
      margin-bottom: 3px;
    }
  }
}

.bubble.is-in {
  .bubble-content {
    &,
    .poll-footer-button {
      border-radius: $bubble-border-radius-medium $bubble-border-radius-big $bubble-border-radius-big $bubble-border-radius-medium;
    }
  }

  &.is-group-first {
    .bubble-content,
    .poll-footer-button {
      border-top-left-radius: $bubble-border-radius-big;
    }
  }

  &.is-group-last {
    &.can-have-tail {
      .bubble-content,
      .poll-footer-button {
        border-bottom-left-radius: 0 !important;
      }

      .bubble-tail {
        margin-left: -8.4px;
      }
  
      /* .bubble-content:after {
        margin-left: -8.4px;
        background-image: url('assets/img/msg-tail-left.svg');
      } */
    }

    &:not(.can-have-tail) {
      .bubble-content, 
      .poll-footer-button {
        border-bottom-left-radius: $bubble-border-radius-big;
      }
    }
  }

  &.just-media {
    .floating-part {
      left: calc(100% + 10px);

      @include respond-to(handhelds) {
        left: calc(100% + 1px);
      }
    }
  }
  
  .quote .webpage-name, 
  .reply-title
  /* , .reply i */ {
    color: var(--primary-color);
  }
  
  .time {
    margin-left: -3px;
    padding-right: 8px;

    .inner {
      margin-bottom: 4px;
    }
  }

  &.is-message-empty.is-group-last {
    /* &:not(.with-media-tail):not(.with-replies) {
      &.photo, &.video {
        .bubble-content {
          border-bottom-left-radius: 6px;
        }
      }
    } */

    &.with-media-tail {
      .attachment {
        border-bottom-left-radius: 0;
      }

      .bubble__media-container {
        margin-left: -9px;
      }
    }
  }

  .audio-subtitle, 
  .contact-number, 
  .audio-time {
    color: var(--secondary-text-color) !important;
  }

  /* .poll {
    &-answer-selected {
      &:before {
        margin-left: -1px;
      }
    }
  } */

  pre, 
  code {
    color: var(--monospace-text-color);
  }

  &:not(.just-media) {
    .reply {
      &.is-overriding-color {
        .reply-border {
          background-color: rgb(var(--override-color));
        }

        .reply-title {
          color: rgb(var(--override-color));
        }

        @include hover() {
          background-color: rgba(var(--override-color), #{$hover-alpha});
        }
      }
    }

    // &:not(.is-group-first) {
    //   .reply {
    //     border-top-left-radius: .25rem;
    //   }
    // }
  }
}

.bubble.is-out {
  flex-direction: row-reverse;
  --message-background-color: var(--message-out-background-color);
  --light-message-background-color: var(--light-message-out-background-color);
  --dark-message-background-color: var(--dark-message-out-background-color);
  --link-color: var(--message-out-link-color);
  --message-primary-color: var(--message-out-primary-color);
  --light-filled-message-primary-color: var(--light-filled-message-out-primary-color);
  --selection-background-color: var(--message-out-selection-background-color);
  --message-time-color: var(--message-out-time-color);
  --message-status-color: var(--message-out-status-color);
  --link-color: var(--message-primary-color);

  .bubble-content {
    margin-left: auto;

    &, 
    .poll-footer-button {
      border-radius: $bubble-border-radius-big $bubble-border-radius-medium $bubble-border-radius-medium $bubble-border-radius-big;
    }

    .name {
      color: var(--message-out-primary-color);
    }

    /* html:not(.is-firefox) */ &-wrapper {
      @include respond-to(medium-screens) {
        transform: scale(1) translateX(calc((var(--chat-input-size) + #{$btn-send-margin}) * -1));

        &.zoom-fade {
          transform: scale3d(.8, .8, 1) translateX(calc((var(--chat-input-size) + #{$btn-send-margin}) * -1));
        }

        @include animation-level(2) {
          transition: var(--bubble-transition-out);
        }

        //@include respond-to(no-floating-left-sidebar) {
          body.is-right-column-shown & {
            transform: scale(1) translateX(0);
            
            @include animation-level(2) {
              transition: var(--bubble-transition-in);
            }
  
            &.zoom-fade {
              transform: scale3d(.8, .8, 1) translateX(0);
            }
          }
        //}
      }
    }
  }

  /* .bubble-content-wrapper {
    > .user-avatar {
      left: auto;
      right: -2.5rem;
    }
  } */
    
  &.is-group-first {
    .bubble-content, 
    .poll-footer-button {
      border-top-right-radius: $bubble-border-radius-big;
    }
  }
    
  &.is-group-last {
    &.can-have-tail {
      .bubble-content, 
      .poll-footer-button {
        border-bottom-right-radius: 0 !important;
      }

      .bubble-tail {
        right: -8.4px;
        transform: translateY(1px) scaleX(-1);
      }
      
      /* .bubble-content:after {
        right: -8.4px;
        background-image: url('assets/img/msg-tail-right.svg');
      } */
    }

    &:not(.can-have-tail) {
      .bubble-content, 
      .poll-footer-button {
        border-bottom-right-radius: $bubble-border-radius-big;
      }
    }
  }

  &.just-media {
    .floating-part {
      right: calc(100% + 10px);
    }

    .message {
      right: 0;
    }
  }

  &:not(.just-media) {
    .reply {
      &-border {
        background-color: var(--message-out-primary-color) !important;
      }

      &-title,
      i {
        color: var(--message-out-primary-color) !important;
      }
    }

    // &:not(.is-group-first) {
    //   .reply {
    //     border-top-right-radius: .25rem;
    //   }
    // }
  }

  .quote:before {
    background-color: var(--message-out-primary-color);
  }

  .quote .webpage-name {
    color: var(--message-out-primary-color);
  }

  .time {
    padding-right: 5px;
    margin-left: -4px;

    .inner {
      bottom: 4px;
    }

    &:after,
    .inner:after {
      font-size: calc(var(--messages-text-size) + 3px);
      //vertical-align: middle;
      margin-left: 1px;
      line-height: var(--messages-time-text-size); // of message
    }
  }

  /* &.is-message-empty .time:after {
    margin-bottom: 1px;
  } */
  
  &.forwarded {
    .name {
      color: var(--message-out-primary-color);
    }
  }
  
  &.is-read {
    .time:after, 
    .time .inner:after {
      content: $tgico-checks;
    }
  }
  
  &.is-sent {
    .time:after, 
    .time .inner:after {
      content: $tgico-check;
    }
  }

  &.is-sending {
    .time:after, 
    .time .inner:after {
      content: $tgico-sending;
    }
  }

  &.is-error {
    .time:after, 
    .time .inner:after {
      content: $tgico-sendingerror;
      color: var(--message-error-color);
    }
  }

  /* &.is-reply .name {
    display: none;
  } */

  .document-ico:after {
    border-top-color: var(--message-background-color);
    border-right-color: var(--message-background-color);
  }

  .audio {
    &-waveform {
      &-bar {
        fill: var(--message-out-primary-color);

        &.active {
          fill: var(--message-out-primary-color) !important;
        }
      }
    }

    &-time,
    &-subtitle {
      color: var(--message-out-status-color);
    }

    &-toggle,
    &-download,
    &.corner-download .preloader-container {
      background-color: var(--message-out-primary-color);
    }

    &-download:empty {
      display: none;
    }

    &.is-unread {
      .audio-waveform-bar {
        fill: var(--message-out-primary-color);
      }
      
      .audio-time:after {
        background-color: var(--message-out-primary-color);
      }
    }

    &-toggle {
      .part {
        background-color: var(--message-out-audio-play-button-color);
      }
    }
  }

  &.is-message-empty.is-group-last {
    /* &:not(.with-media-tail) {
      &.photo, &.video {
        .bubble-content {
          border-bottom-right-radius: 6px;
        }
      }
    } */

    &.with-media-tail {
      .attachment {
        border-bottom-right-radius: 0;
      }

      .bubble__media-container {
        margin-right: -9px;
      }
    }
  }

  &.is-message-empty {
    .reactions-block {
      justify-content: flex-end;
    }

    .reaction-block {
      margin-right: .25rem;

      &:last-child {
        margin-right: 0;
      }

      &.is-chosen {
        --chosen-background-color: var(--surface-color);
      }
    }

    &:not(.emoji-big) {
      .reactions-block {
        margin-left: auto;
      }
    }
  }

  .contact-number, 
  .document-size,
  .bubble-call-subtitle {
    color: var(--message-out-status-color);
  }

  .bubble-call-subtitle:before {
    transform: rotate(-45deg);
  }

  poll-element {
    .poll {
      &-desc, 
      &-votes-count {
        color: var(--message-out-primary-color);
      }

      &-line use {
        stroke: var(--message-out-primary-color);
      }

      &-answer-selected {
        background-color: var(--message-out-primary-color);
        color: var(--light-filled-message-out-primary-color);
      }

      html.no-touch &-answer:hover {
        .animation-ring {
          background-color: var(--message-out-primary-color);
          opacity: .08;
        }
      }

      &-footer-button, 
      &-hint {
        color: var(--message-out-primary-color);
      }

      &-avatars {
        .stacked-avatars {
          --border-color: var(--message-background-color);
        }
      }
    }

    .progress-ring__circle {
      stroke: var(--message-out-primary-color);
    }

    .c-ripple__circle {
      background-color: var(--message-out-primary-color);

      &:not(.hiding) {
        opacity: .08;
      }
    }
  }

  &.is-sending poll-element {
    pointer-events: none;
  }

  .audio {
    .progress-line {
      --color: var(--message-out-primary-color);

      &:before, 
      &__loaded {
        background-color: var(--message-out-primary-color);
      }
    }

    .preloader-container {
      --color: var(--message-background-color);
    }
  }

  .bubble-beside-button {
    right: auto;
    left: var(--message-beside-button-margin);
    //transform: scaleX(-1);

    &.goto-original {
      transform: rotate(180deg);
    }
  }

  &.is-multiple-documents {
    .document-container {
      .bubble-select-checkbox {
        background-color: var(--message-background-color);

        &:before {
          border-color: var(--dark-message-background-color);
        }

        .checkbox-box-border {
          border-color: var(--message-background-color);
        }
      }

      /* &:after {
        left: -50vw;
      } */
    }
  }

  .bubble-hover-reaction {
    right: auto;
    left: var(--offset);
  }

  /* &.sticker {
    .message {
      margin-right: 0;
      margin-left: auto;
    }
  } */
}

.reply-markup {
  width: 100%;
  margin-top: .0625rem;

  .bubble:not(.is-group-last) & {
    margin-bottom: $bubble-margin;
  }

  &-row {
    margin-top: $bubble-margin;
    overflow: hidden;
    min-height: 2.5rem;
    display: flex;

    &:last-child {
      .reply-markup-button {
        &:first-child {
          border-bottom-left-radius: $border-radius-big;
        }

        &:last-child {
          border-bottom-right-radius: $border-radius-big;
        }
      }
    } 
  }

  &-button {
    padding: .5625rem 0;
    border-radius: .375rem;
    z-index: 2;
    font-size: var(--font-size-14);
    user-select: none;
    text-align: center;
    color: #fff !important;
    outline: none;
    border: none;
    width: 100%;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    //line-height: var(--line-height);
    background: var(--message-highlightning-color);
    font-weight: var(--font-weight-bold);

    &-text {
      position: relative;
      pointer-events: none;
    }

    &.anchor-url {
      text-decoration: none !important;
    }

    & + & {
      margin-left: $bubble-margin;
    }

    &.tgico:before, 
    .forward-icon {
      position: absolute;
      right: .125rem;
      top: .125rem;
      display: block;
    }

    &.is-link:before {
      content: $tgico-arrow-next;
      transform: rotate(-45deg);
    }

    &.is-buy:before {
      content: $tgico-card;
    }

    &.is-switch-inline:before {
      content: $tgico-forward_filled;
    }
  }
}

.reply-markup-button,
.bubble.just-media .reply,
.bubble-beside-button {
  &:after {
    content: " ";
    display: block;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    border-radius: inherit;
    opacity: 0;
    z-index: -1;
    background-color: #fff;
  }

  @include hover() {
    &:after {
      opacity: var(--hover-alpha);
    }
  }
}

.bubble-primary-color {
  color: var(--message-primary-color);
  font-weight: var(--font-weight-bold);
}
